//
//  CPSCVideoFilterListCategoryView.m
//  CPSCustomer
//
//  Created by codew on 9/1/21.
//  Copyright © 2021 dgg_hzk. All rights reserved.
//

#import "CPSCVideoFilterListCategoryView.h"
#import "CPSCVideoFilterListCategoryCell.h"

@interface CPSCVideoFilterListCategoryView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) NSArray * dataArray;

@end

static NSString * CellIdentifier = @"CPSCVideoFilterListCategoryCell";


@implementation CPSCVideoFilterListCategoryView


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        
        [self initSubviews];
    }
    return self;
}


- (void)initSubviews
{
    [self addSubview:self.collectionView];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    
    [self deBugUI];
}

- (void)deBugUI
{
    //    self.viewHtmlContentSuper.backgroundColor = kCPSCRandomColor;
    //    self.lblContent.backgroundColor = kCPSCRandomColor;
    self.dataArray = @[@"法律", @"工商", @"法学概论-刑法修正案",@"法学概论",@"司法学", @"知识", @"刑法修正案",@"管理经营之道之龙门阵火锅", @""];
    [self.collectionView reloadData];
}

- (void)updateViewWithCategoryArray:(NSArray *)array
{
    
}


#pragma mark - UICollectionViewDelegate

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString * title = self.dataArray[indexPath.row];
    /** 初始宽度是2个字时的, 大于从新计算一下 */
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionViewLayout;
    if (title.length > 2) {
        
        CGSize size = flowLayout.itemSize;
        size.width = [CPSCVideoFilterListCategoryCell widthCellWithTitle:title];
        
        return size;
    }else if (title.length == 0){
        
        // 代表最后一个编辑
        CGSize size = flowLayout.itemSize;
        size.width = 78;
        return size;
        
    }else{
        
        return flowLayout.itemSize;
    }

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString * title = self.dataArray[indexPath.row];
    if (title.length == 0) {
        // 编辑
    }else{
        
        // 普通选择
    }
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _dataArray.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * title = self.dataArray[indexPath.row];
    
    CPSCVideoFilterListCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    [cell updateCellWithItem:title];
    
//    cell.backgroundColor = kCPSCRandomColor;
    return cell;
}

#pragma mark - Getter Setter

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        CGFloat margin = 8;
        CGFloat sectionInsetLR = 16;
        CGFloat sectionInsetTB = 8;
        
        /** 初始宽度是2个字时的 */
        CGFloat cellWidth = 60;
        CGFloat cellHeight = 32;
        
        flowLayout.itemSize = CGSizeMake(cellWidth, cellHeight);
        flowLayout.minimumInteritemSpacing = margin;
        flowLayout.minimumLineSpacing = margin;
//        UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
        flowLayout.sectionInset = UIEdgeInsetsMake(sectionInsetTB, sectionInsetLR, sectionInsetTB, sectionInsetLR);
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
                        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
                
        
        [_collectionView registerClass:[CPSCVideoFilterListCategoryCell class] forCellWithReuseIdentifier:NSStringFromClass([CPSCVideoFilterListCategoryCell class])];
    }
    
    return _collectionView;
}



@end
